// xc_lshop/distr/manage/smorder.js
const app = getApp()
var actbill = require('../model/distribution_smapi.js')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    showtext:"",
    isload: true,
    staticdata:null,
    CustomBar: app.globalData.CustomBar,
    active: "3",
    topnav:[{val:"1",name:"前天"},{val:"2",name:"昨天"},{val:"3",name:"今天"},{val:"4",name:"上月"},{val:"5",name:"本月"},{val:"6",name:"累计"}],
    navstatus:"0",
    nav:[{status:"0","name":"全部"},{status:"1","name":"待结算"},{status:"2","name":"已结算"}]
  },
  xcloaddata: function (options, config) {
    this.setConfig(config)
  },
  onclicknav(e){
    var status=e.currentTarget.dataset.status;
    this.setData({
      navstatus:status
    })
    this.getpagelist(1)
  },
  setConfig(config){
    var update = {};
    console.log("config")
    console.log(config)
    if(config.smapiconfig && config.smapiconfig.meituan){
       if(config.smapiconfig.meituan.showtext){
        update['showtext']=config.smapiconfig.meituan.showtext
       }
    }
    // if (config.distribution && config.distribution.distext && config.distribution.distext.center) {
    //   update['xtitle'] = config.distribution.distext.center.disorder
    // }

    this.setData(update)
  },
  getStatic(){
    var that = this;
    actbill.getStatic(this.data.active).then(function (res) {
      if(res.status==1){
        that.setData({
          staticdata:res.obj
        })
      }     
    })
  },
  act(config,userinfo){
    // this.getnav();
    // this.setData({
    //   member:userinfo
    // })
    // this.setConfig(config)
    this.getStatic()
    this.getpagelist(1)
  },
  getpagelist(rest, strwhere) {
    var that = this
    if (!strwhere) {
      strwhere = {}
    }
    strwhere["type"] = this.data.active
    strwhere["status"] = this.data.navstatus
    if (rest == 1) {
      that.setData({
        modellist: []
      });
    }
    this.setData({ isload: true })
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      updatemdoel["isload"] = false

      that.setData(updatemdoel);
    })
    return;
  },
  afterlogin: function (options, config, userinfo) {
    console.log("userinfo")
    console.log(userinfo)
    if(userinfo['memberinfo'] && userinfo['memberinfo']['is_distribution']==1){
      this.act(config,userinfo)
    }else{
      var link="";
      if(userinfo['memberinfo']){
        link="/xc_lshop/distr/info/apply"
      }
      this.pageback(link)
    }
  },
  onChange(e) {
     this.setData({
       active:e.detail.name
     })
     this.getStatic()
     this.getpagelist(1)
  },
 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getpagelist(-1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})